package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.auto.*;
import com.model.auto.vo.GetSchoolV2Vo;
import com.service.ISchoolLabelService;
import com.service.ISubjectMajorService;
import com.service.ISubjectSchoolService;
import com.util.R;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 选科相关
 *
 * @author zhaoyifan
 * @date 2024/8/13 17:36
 */
@RestController
@RequestMapping("/subject")
public class SubjectSchoolController {

    @Autowired
    ISubjectSchoolService schoolService;
    @Autowired
    ISubjectMajorService majorService;
    @Autowired
    private ISchoolLabelService schoolLabelService;

    /**
     * 获取选科学校列表
     *
     * @param getSchoolVo
     * @return
     */
    @PostMapping("/school/getList")
    public R getList(@RequestBody GetSchoolV2Vo getSchoolVo) {
        LambdaQueryWrapper<SubjectSchool> query = new LambdaQueryWrapper<SubjectSchool>()
                .like(!StringUtil.isEmpty(getSchoolVo.getSchool()), SubjectSchool::getSchoolName, getSchoolVo.getSchool());

        //判断是否有查询条件
        if (StringUtil.isEmpty(getSchoolVo.getSchool()) && StringUtil.isEmpty(getSchoolVo.getMajor1()) && StringUtil.isEmpty(getSchoolVo.getMajor2()) && StringUtil.isEmpty(getSchoolVo.getMajor3())
                && StringUtil.isEmpty(getSchoolVo.getQualifications()) && StringUtil.isEmpty(getSchoolVo.getHierarchys()) && ObjectUtils.isEmpty(getSchoolVo.getNature())
                && ObjectUtils.isEmpty(getSchoolVo.getType()) && ObjectUtils.isEmpty(getSchoolVo.getCity()) && ObjectUtils.isEmpty(getSchoolVo.getMajorType())
                && ObjectUtils.isEmpty(getSchoolVo.getSubject()) && StringUtil.isEmpty(getSchoolVo.getKeywords())) {
            List<SubjectSchool> result = schoolService.list(query);
            R r = R.ok(result);
            r.put("searchTotal", result.size());
            return r;
        }
        Boolean queryMajor = false;
        LambdaQueryWrapper<SubjectMajor> subjectQuery = new LambdaQueryWrapper<SubjectMajor>().select(SubjectMajor::getSchoolName);

        if(Objects.nonNull(getSchoolVo.getMajorType())){
            List<String> subject1 = new ArrayList<>();
            subject1.add("不限");
            if (getSchoolVo.getMajorType() == 1) {
                subject1.add("物理");
            } else if (getSchoolVo.getMajorType() == 2) {
                subject1.add("历史");
            }
            subjectQuery.in(SubjectMajor::getSubjectFirst, subject1);
            queryMajor=true;
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
            List<String> subject2 = new ArrayList<>();
            subject2.add("不限");
            subject2.addAll(getSchoolVo.getSubject());
            for (String subjectParam : getSchoolVo.getSubject()) {
                subject2.add(subjectParam);
            }
            if(getSchoolVo.getSubject().size()>1){
                subject2.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
                subject2.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
            }
            subjectQuery.in(SubjectMajor::getSubjectSecond, subject2);
            queryMajor=true;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())) {
            subjectQuery.and(wrapper -> wrapper.like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), SubjectMajor::getMajorName, getSchoolVo.getMajor1())
                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), SubjectMajor::getMajorName, getSchoolVo.getMajor2())
                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), SubjectMajor::getMajorName, getSchoolVo.getMajor3()));
            queryMajor=true;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getKeywords())) {
            subjectQuery.and(wrapper -> wrapper.like(!StringUtil.isEmpty(getSchoolVo.getKeywords()), SubjectMajor::getKeywords, getSchoolVo.getKeywords()));
            queryMajor=true;
        }

        if(queryMajor){
            List<SubjectMajor> majorList = majorService.list(subjectQuery.groupBy(SubjectMajor::getSchoolName));
            if (ObjectUtils.isEmpty(majorList)) {
                return R.ok(new ArrayList<>());
            }
            query.in(SubjectSchool::getSchoolName, majorList.stream().map(u->u.getSchoolName()).collect(Collectors.toList()));
        }

        List<String> codeList = new ArrayList<>();
        //办学性质||院校层级||博硕资格不为空||城市不为空
        if (!ObjectUtils.isEmpty(getSchoolVo.getNature()) || !ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications()) || !ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            List<String> labelCode = new ArrayList<>();
            if (!ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications())) {
                List<SchoolLabel> labelList = schoolLabelService.getByParam(String.join("|", getSchoolVo.getHierarchys()), String.join("|", getSchoolVo.getQualifications()));
                labelList.forEach(i -> labelCode.add(i.getNewCode()));
            }
            List<SchoolLabel> schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                    .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
                    .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
                    .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                    .select(SchoolLabel::getNewCode)
            );
            codeList = schoolLabels.stream().map(SchoolLabel::getNewCode).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(codeList)) {
                return R.ok(new ArrayList<>());
            }
            query.in(SubjectSchool::getCode, codeList);
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getType())) {
            query.in(SubjectSchool::getSchoolType, getSchoolVo.getType());
        }

        List<SubjectSchool> result = schoolService.list(query);
        if(CollectionUtils.isEmpty(result)){
            return R.ok(new ArrayList<>());
        }
        return R.ok(result);
    }

    /**
     * 获取选科学校列表
     *
     * @param getSchoolVo
     * @return
     */
    @PostMapping("/school/getPageList")
    public R getPageList(@RequestBody GetSchoolV2Vo getSchoolVo) {
        LambdaQueryWrapper<SubjectSchool> query = new LambdaQueryWrapper<SubjectSchool>()
                .eq(!StringUtil.isEmpty(getSchoolVo.getSchool()), SubjectSchool::getSchoolName, getSchoolVo.getSchool());

        //判断是否有查询条件
        if (StringUtil.isEmpty(getSchoolVo.getSchool()) && StringUtil.isEmpty(getSchoolVo.getMajor1()) && StringUtil.isEmpty(getSchoolVo.getMajor2()) && StringUtil.isEmpty(getSchoolVo.getMajor3())
                && StringUtil.isEmpty(getSchoolVo.getQualifications()) && StringUtil.isEmpty(getSchoolVo.getHierarchys()) && ObjectUtils.isEmpty(getSchoolVo.getNature())
                && ObjectUtils.isEmpty(getSchoolVo.getType()) && ObjectUtils.isEmpty(getSchoolVo.getCity()) && ObjectUtils.isEmpty(getSchoolVo.getMajorType())
                && ObjectUtils.isEmpty(getSchoolVo.getSubject()) && StringUtil.isEmpty(getSchoolVo.getKeywords())) {
            List<SubjectSchool> result = schoolService.list(query);
            R r = R.ok(result);
            r.put("searchTotal", result.size());
            return r;
        }
        Boolean queryMajor = false;
        LambdaQueryWrapper<SubjectMajor> subjectQuery = new LambdaQueryWrapper<SubjectMajor>().select(SubjectMajor::getSchoolName);

        if(Objects.nonNull(getSchoolVo.getMajorType())){
            List<String> subject1 = new ArrayList<>();
            subject1.add("不限");
            if (getSchoolVo.getMajorType() == 1) {
                subject1.add("物理");
            } else if (getSchoolVo.getMajorType() == 2) {
                subject1.add("历史");
            }
            subjectQuery.in(SubjectMajor::getSubjectFirst, subject1);
            queryMajor=true;
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
            List<String> subject2 = new ArrayList<>();
            subject2.add("不限");
            subject2.addAll(getSchoolVo.getSubject());
            if(getSchoolVo.getSubject().size()>1){
                subject2.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
                subject2.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
            }
            subjectQuery.in(SubjectMajor::getSubjectSecond, subject2);
            queryMajor=true;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())) {
            subjectQuery.and(wrapper -> wrapper.like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), SubjectMajor::getMajorName, getSchoolVo.getMajor1())
                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), SubjectMajor::getMajorName, getSchoolVo.getMajor2())
                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), SubjectMajor::getMajorName, getSchoolVo.getMajor3()));
            queryMajor=true;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getKeywords())) {
            subjectQuery.and(wrapper -> wrapper.like(!StringUtil.isEmpty(getSchoolVo.getKeywords()), SubjectMajor::getKeywords, getSchoolVo.getKeywords()));
            queryMajor=true;
        }

        if(queryMajor){
            List<SubjectMajor> majorList = majorService.list(subjectQuery.groupBy(SubjectMajor::getSchoolName));
            if (ObjectUtils.isEmpty(majorList)) {
                return R.ok(new ArrayList<>());
            }
            query.in(SubjectSchool::getSchoolName, majorList.stream().map(u->u.getSchoolName()).collect(Collectors.toList()));
        }

        List<String> codeList = new ArrayList<>();
        //办学性质||院校层级||博硕资格不为空||城市不为空
        if (!ObjectUtils.isEmpty(getSchoolVo.getNature()) || !ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications()) || !ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            List<String> labelCode = new ArrayList<>();
            if (!ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications())) {
                List<SchoolLabel> labelList = schoolLabelService.getByParam(String.join("|", getSchoolVo.getHierarchys()), String.join("|", getSchoolVo.getQualifications()));
                labelList.forEach(i -> labelCode.add(i.getNewCode()));
            }
            List<SchoolLabel> schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                    .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
                    .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
                    .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                    .select(SchoolLabel::getNewCode)
            );
            codeList = schoolLabels.stream().map(SchoolLabel::getNewCode).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(codeList)) {
                return R.ok(new ArrayList<>());
            }
            query.in(SubjectSchool::getCode, codeList);
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getType())) {
            query.in(SubjectSchool::getSchoolType, getSchoolVo.getType());
        }
        PageInfo<Object> result = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize())
                .doSelectPageInfo(() -> {
                    List<SubjectSchool> list = schoolService.list(query);
                });

        if(CollectionUtils.isEmpty(result.getList())){
            return R.ok(new ArrayList<>()).put("searchTotal", 0);
        }
        return R.ok(result.getList()).put("searchTotal", result.getTotal());
    }

    /**
     * 获取院校专业
     *
     * @param getSchoolVo
     * @return
     */
    @PostMapping("/major/getList")
    public R getMajorList(@RequestBody GetSchoolV2Vo getSchoolVo) {
        LambdaQueryWrapper<SubjectMajor> query = new LambdaQueryWrapper<SubjectMajor>()
                .eq(!StringUtil.isEmpty(getSchoolVo.getSchoolName()), SubjectMajor::getSchoolName, getSchoolVo.getSchoolName());

        if(!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())){
            query.and(wrapper -> wrapper.like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), SubjectMajor::getMajorName, getSchoolVo.getMajor1()).or()
                    .like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), SubjectMajor::getMajorName, getSchoolVo.getMajor2()).or()
                    .like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), SubjectMajor::getMajorName, getSchoolVo.getMajor3()));
        }
        if (!StringUtil.isEmpty(getSchoolVo.getKeywords())) {
            query.and(wrapper -> wrapper.like(!StringUtil.isEmpty(getSchoolVo.getKeywords()), SubjectMajor::getKeywords, getSchoolVo.getKeywords()));
        }
        if(Objects.nonNull(getSchoolVo.getMajorType())){
            List<String> subject1 = new ArrayList<>();
            subject1.add("不限");
            if (getSchoolVo.getMajorType() == 1) {
                subject1.add("物理");
            } else if (getSchoolVo.getMajorType() == 2) {
                subject1.add("历史");
            }
            query.in(SubjectMajor::getSubjectFirst, subject1);
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
            List<String> subject2 = new ArrayList<>();
            subject2.add("不限");
            subject2.addAll(getSchoolVo.getSubject());
            if(getSchoolVo.getSubject().size()>1){
                subject2.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
                subject2.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
            }
            query.in(SubjectMajor::getSubjectSecond, subject2);
        }

        List<SubjectMajor> result = majorService.list(query);
        return R.ok(result);
    }

    /**
     * 获取全部专业名称
     * @return
     */
    @GetMapping("/major/getAllList")
    public R getAllList() {
        LambdaQueryWrapper<SubjectMajor> query = new LambdaQueryWrapper<SubjectMajor>()
                .select(SubjectMajor::getMajorName)
                .isNotNull(SubjectMajor::getMajorName)
                .groupBy(SubjectMajor::getMajorName);
        List<SubjectMajor> result = majorService.list(query);
        List<String> names = result.stream().map(major->major.getMajorName()).collect(Collectors.toList());
        return R.ok(names);
    }
}
